Ext.namespace("com.test.cmp");
com.test.cmp.UserGrid = Ext.extend(Ext.grid.GridPanel, {
	title : '客户列表',
	viewConfig : {
		forceFit : true
	},
	loadMask : true,
	userDetailWindow : null,
	/**
	 * initComponent
	 * 
	 * @param {}
	 *            config
	 */
	initComponent : function(config) {
		window.delUser = function(userId) {
			alert("开始删除" + userId);
		}

		window.showDetail = function(userId) {
			alert("显示详情" + userId);
		}

		this.ageRenderer = function(val) {
			if (val > 25) {
				return "<font color='#ff0000'>" + val + "</font>";
			}
			return "<font color='#0000ff'>" + val + "</font>";
		};

		this.actionRenderer = function(val) {
			var htmlStr = '<a href="#" onclick="showDetail(\'' + val
					+ '\')">详情</a>';
			htmlStr += "&nbsp;&nbsp;&nbsp;&nbsp;";
			htmlStr += '<a href="#" onclick="delUser(\'' + val + '\')">删除</a>';
			return htmlStr;
		};

		this.store = new Ext.data.JsonStore({
			url : 'http://localhost:8080/dmf/data/usermng/userData1.json',
			autoLoad : true,
			totalProperty : 'totalCount',
			root : "users",
			fields : [ {
				name : 'userName'
			}, {
				name : 'age'
			}, {
				name : 'birth'
			}, {
				name : 'addr'
			}, {
				name : 'company'
			}, {
				name : 'duty'
			} ]
		});
		this.sm = new Ext.grid.CheckboxSelectionModel();
		this.columns = [ this.sm, new Ext.grid.RowNumberer(), {
			header : '姓名',
			dataIndex : 'userName'
		}, {
			header : '年龄',
			dataIndex : 'age',
			renderer : this.ageRenderer
		}, {
			header : '出身日期',
			dataIndex : 'birth'
		}, {
			header : '地址',
			dataIndex : 'addr'
		}, {
			header : '公司',
			dataIndex : 'company'
		}, {
			header : '职责',
			dataIndex : 'duty'
		}, {
			header : '操作',
			dataIndex : 'userName',
			renderer : this.actionRenderer
		} ];
		this.bbar = new Ext.PagingToolbar({
			pageSize : 20,
			store : this.store,
			displayInfo : true,
			buttons : [ '-', {
				text : "客户详情",
				handler : this.showUserDetail.createDelegate(this)
			} ]
		});

		com.test.cmp.UserGrid.superclass.initComponent.call(this);
	},
	loadData : function(dataUrl) {
		alert("dataUrl = " + dataUrl);
		this.store.proxy.setUrl(dataUrl);
		this.store.load();
	},
	removeAllData : function() {
		this.store.removeAll();
	},
	showUserDetail : function() {
		var selRecord = this.getSelectionModel().getSelected();
		if (!selRecord) {
			Ext.Msg.show({
				title : '提示',
				msg : "请选中一条记录",
				buttons : Ext.Msg.OKCANCEL,
				icon : Ext.Msg.WARNING
			});
			return;
		}
		if (!this.userDetailWindow) {
			this.userDetailWindow = new com.test.cmp.UserDetailWindow();
		}
		this.userDetailWindow.setData(selRecord);
		this.userDetailWindow.show();
	}
});
Ext.reg("usergrid", com.test.cmp.UserGrid)